<?php
class pgDatabase
        {
        var $transaction; //whether we are using transaction for the query or not
        var $host; //computer name
        var $dbname; //database name
        var $user; 
        var $password; 
        var $conn;
        var $result;
        var $search_path; //to set the schema in which the query will look for database objects
        var $transaction_isolation; //transaction related parameter used by the database engine
        var $lock_type; //lock used for the data
        var $query;
        var $errors;

        function pgDatabase($user='sundar', $password='', $host='localhost', $dbname='leatherlinkdb_development', $search_path='public')
                {
                $this->user=$user;
                $this->password=$password;
                $this->host=$host;
                $this->dbname=$dbname;
                $this->search_path=$search_path;
                }

        function connect()
                {
                $this->conn=mysql_pconnect($this->host, $this->user, $this->password);

            
		if (mysql_select_db($this->dbname, $this->conn) === false) 
                                {
                                $this->errors="1000"; //print it after doCentreHtml
                                }
                }

        function setSearchPath()
                {
               // $this->search_path=($this->search_path)?$this->search_path:'public';
                //$this->query="SET search_path=".mysql_escape_string($this->search_path);
                //$this->runQuery();
                }

        function beginTransaction()
                {
                //$this->transaction=FALSE;
                //$this->query="START TRANSACTION ";
                //$this->query.=($this->transaction_isolation)?" ISOLATION LEVEL ".$this->transaction_isolation:"";
                //$this->query.=($this->lock_type)?$this->lock_type:"";
                //$this->runQuery();
                //$this->transaction=TRUE;
                }
	function privilegeQuery($table_name_array,$user_id,$is_condition)
		{
                $value=implode("','",$table_name_array);
		$this->query="SELECT getPrivilegeString(ARRAY['$value'],".$user_id.",'".$this->query."','".$is_condition."');";
		$this->runQuery();
		$temp=$this->returnArrays();
		return $temp[0]["getprivilegestring"];
		}
        function runQuery()
                {
                if(!$this->conn)
                        {
                        $this->connect();
                        }
                $this->errors="";
                $this->query.=";";

		$this->result = mysql_query($this->query,$this->conn);
                if (!is_resource($this->result))
                        {
			echo "<div class=\"error\">".$global_pgsql_locale->ERRORS["global_pgsql.inc"]["query_exec"]."</div>";
  			$debugging=debug_backtrace();

			foreach ($debugging as $debug)
				{
				echo "File = ".$debug["file"]."<br />";
				echo "Line = ".$debug["line"]."<br />";
				echo "Function = ".$debug["function"]."<br />";
				}
                        echo $this->query;

                        }

                unset($this->query);
                if (!is_resource($this->result))
                        {
                        //$local_errors = $this->errors=mysql_result_error($this->result);
                        if($this->transaction)
                                {
                    //            $this->transaction=FALSE;
                          //      $this->query="ROLLBACK TRANSACTION";
                  //              $this->runQuery();
                                }
                        }
		if($local_errors)
			$this->errors=$local_errors;
                }

        function endTransaction()
                {
                if($this->transaction)
                        {
//                         $this->transaction=FALSE;
//                         $this->query="COMMIT TRANSACTION";
//                         $this->runQuery();
                        }
                }

        function returnArrays()
                {
                if (mysql_num_rows($this->result) > 0)
                        {
                        while($row=mysql_fetch_assoc($this->result))
                                {
                                $arrays[]=$row;
                                }
                        }
                return $arrays;
                }
	/*****************************************************************************************
        Function Name         :getConditionalColumn
        No. of Arguments        :Nil
        Arguments                :Nil
        Action                        :takes the table name and column name and condition arrays as arguments and                         returns the value of the column - to be used to get value of column matching a single
                                condition
        Returns                        :column value
        Created By                :Ma SivaKumar
        Created On                :10 August 2004
        *****************************************************************************************/

        function getConditionalColumn($table_name,$column_name, $condition_array)
                {
                $name=key($condition_array);
                $value=$condition_array[$name];
                $this->query="select $column_name as \"result\" from $table_name where $name='$value';";
                $this->runQuery();
                $column_name_array=$this->returnArrays();
                $column_name=$column_name_array[0]["result"];
                return $column_name;
                }

        }

class pgSql 
        {
        var $column; //what columns to be queried or set or deleted
        var $parent_only; //whether to restrict the query to only one table
        var $table;
        var $join_condition;
        var $condition;
        var $group_by;
        var $having;
        var $order_by;
        var $limit;
        var $offset_by;
        var $sub_query;
        var $query;
        var $exclude_columns="";
        
       function resetQuery()
                {
                unset($this->column,$this->parent_only,$this->table,$this->join_condition,$this->condition,$this->group_by,$this->having,$this->order_by,$this->limit,$this->offset_by,$this->sub_query,$this->exclude_columns);
                }
                
        function generateInsertQuery()
                {
                trimValues($this->column);
                addQuotes($this->column,$this->exclude_columns);
                $this->query="INSERT INTO ".$this->table;
                $this->query.=($this->column)?" (".implode (',', array_keys($this->column)).") VALUES (".implode (',', array_values($this->column)).");":"";
                $this->query.=($this->sub_query)?$this->sub_query:"";
                $this->resetQuery();
                }

        function generateUpdateQuery()
                {
                trimValues($this->column);
                addQuotes($this->column,$this->exclude_columns);
                $this->query="UPDATE ".$this->parent_only." ".$this->table." SET ".implode(',',assoc_to_num_array($this->column,'='));
                $this->query.=($this->condition)?" WHERE ".((is_array($this->condition))?implode(' AND ',$this->condition):$this->condition):"";
                $this->resetQuery();
                }
                
        function generateDeleteQuery()
                {
                $this->query="DELETE FROM ".$this->parent_only." ".$this->table;
                $this->query.=($this->condition)?" WHERE ".((is_array($this->condition))?implode(' AND ',$this->condition):$this->condition):"";
                $this->resetQuery();
                }
                
        function generateSelectQuery()
                {
                $this->query="SELECT ";
                $this->query.=($this->column)?(is_array($this->column))?implode(',',$this->column):$this->column:" * ";
                $this->query.=" FROM ".$this->parent_only." ".$this->table." ";
                $this->query.=($this->join_condition)?" ".((is_array($this->join_condition))?implode(' ',$this->join_condition):$this->join_condition):"";
                $this->query.=($this->condition)?" WHERE ".((is_array($this->condition))?implode(' AND ',$this->condition):$this->condition):"";
                $this->query.=($this->group_by)?" GROUP BY ".((is_array($this->group_by))?implode(', ',$this->group_by):$this->group_by):"";
                $this->query.=($this->having)?" HAVING ".((is_array($this->having))?implode(' AND ',$this->having):$this->having):"";
                $this->query.=($this->order_by)?" ORDER BY ".((is_array($this->order_by))?implode(', ',$this->order_by):$this->order_by):"";
                $this->query.=($this->limit)?" LIMIT ".$this->limit:"";
                $this->query.=($this->offset_by)?" OFFSET ".$this->offset_by:"";
                $this->resetQuery();
                }

        }
?>
